<template>
  <el-dialog
    title="还款上传"
    :visible.sync="dialogVisible"
    width="650px"
  >
    <el-button
      type="primary"
      @click="addJH()"
    >增加计划</el-button>
    <el-form
      :inline="true"
      :model="formData"
      class="demo-form-inline"
      label-width="100px"
    >
      <el-form-item label="合同编号">
        <el-input
          v-model="contract_no"
          placeholder="合同编号"
        />
      </el-form-item>
      <div v-for="(item,index) of formData.repay_record" :key="index">
        <div style="margin:30px;color:red;font-size:26px">第{{ index+1 }}
          <el-button
            type="primary"
            @click="deleteJH(index)"
          >删除计划</el-button>
        </div>

        <el-form-item label="期数">
          <el-input
            v-model="item.period"
            placeholder="期数"
          />
        </el-form-item>
        <el-form-item label="还款金额">
          <el-input
            v-model="item.repay_amount"
            placeholder="还款金额"
          />
          元
        </el-form-item>
        <el-form-item label="还款时间">
          <el-date-picker
            v-model="item.repay_time"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
      </div>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Levinwang_Repay } from '@/api/marketing'
export default {
  props: {
    dialogTitle: {
      type: String
    },
    selectRow: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      formData: {
        repay_record: []
      },
      default_item: {
        period: 0,
        contract_no: '',
        repay_amount: '',
        repay_time: ''
      },
      contract_no:''
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.getData()
      }
    }
  },
  methods: {
    addJH() {
      this.formData.repay_record.push(JSON.parse(JSON.stringify(this.default_item)))
    },
    deleteJH(index) {
      this.formData.repay_record.splice(index, 1)
    },
    submit() {

       console.log(this.formData)

         let arr =[];
       arr= JSON.parse(JSON.stringify(this.formData));
          console.log(arr);
      for (let index = 0; index < arr.repay_record.length; index++) {
        arr.repay_record[index].contract_no = this.contract_no
        arr.repay_record[index].repay_amount *= 100
      }
      console.log(this.formData);
      // this.formData = { 'repay_record': [
      //   {
      //     'contract_no': 'HT-TEST001',
      //     'period': 1,
      //     'repay_amount': 600,
      //     'repay_time': '2021-11-01 10:00:20'
      //   },
      //   {
      //     'contract_no': 'HT-TEST001',
      //     'period': 2,
      //     'repay_amount': 600,
      //     'repay_time': '2021-11-01 12:00:30'
      //   }
      // ]
      // }

      Levinwang_Repay({ 'order_id': this.selectRow.id, 'data':arr }).then(res => {
        this.dialogVisible = false
        this.$message({
          message: '还款成功',
          type: 'success'
        })
        this.formData={
          repay_record: []
        }
      })
    },
    getData() {
      this.contract_no = this.selectRow.trade_no
      // autoRechargeInfo(this.type).then(res => {
      //   this.infoData = { ...res } || {}
      //   this.calculate()
      // })
    },
    changeNumberInput(key, value) {
      const pattern = /^[1-9][0-9]*$/ // 正整数的正则表达式
      // 不符合正整数时
      if (!pattern.test(value)) {
        // input 框绑定的内容为空
        this.formData[key] = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.form_info {
  padding: 20px 30px;
  background: #f5f5f5;
  /deep/.el-divider--horizontal {
    margin: 10px 0;
  }
}
.suffix {
  width: 28px;
  height: 28px;
  background: #e6e6e6;
  border-radius: 0px 3px 3px 0px;
  box-sizing: border-box;
  line-height: 28px;
  text-align: center;
}
.check_form {
  /deep/.el-checkbox__inner {
    border-color: #ff4146;
  }
}
.form_item {
  display: flex;
  line-height: 35px;
  & > label {
    width: 100px;
    color: #666666;
  }
  /deep/.el-input__inner {
    height: 28px;
    border-radius: 3px 0px 0px 3px;
  }
  /deep/.el-input__icon {
    line-height: 28px;
  }
}
</style>
